<!-- 初审 -->
<template>
  <div>
    <BaseSearch :config="searchConfig" v-bind="form" ref="search"></BaseSearch>
    <BaseTable :columns="columns" v-bind="table"></BaseTable>
    <CreditExtensionPop ref="CreditExtensionPop" @closePop="addEditSuccess"></CreditExtensionPop>
  </div>
</template>

<script>
import BaseSearch from '@/components/BaseSearch/index.vue'
import BaseTable from '@/components/BaseTable/index.vue'
import { FormTableMixin } from '@/mixins/form-table'
import http from '@/utils/request'
import CreditExtensionPop from '../../components/CreditExtensionPop.vue'
import { CREDITEXTENSION_OFFLINE_STATUS } from '@/constants/auditModel.js'

const service = async params => {
  const { records, total } = await http.get('/credit_applys', {
    params: { ...params, applyType: 1, listType: 1 },
  })

  return {
    data: records,
    total: total,
  }
}
export default {
  name: 'ApplyPreliminary',
  components: { BaseSearch, BaseTable, CreditExtensionPop },
  mixins: [FormTableMixin(service)],
  data() {
    const isCanAudit = this.$checkAccess('MENU_CREDIT_EXTENSION_FIRST_TRIAL_APPROVAL')
    const isCanDetail = this.$checkAccess('MENU_CREDIT_EXTENSION_FIRST_TRIAL_DETAIL')
    return {
      CREDITEXTENSION_OFFLINE_STATUS,
      searchConfig: [
        { component: 'input', prop: 'searchStr', label: '搜索', placeholder: '企业名称/统一信用代码' },
        { component: 'select', prop: 'appCode', label: '来源', options: [] },
      ],
      columns: [
        { label: '企业名称', prop: 'tenantName' },
        { label: '统一信用代码', prop: 'uscCode' },
        { label: '产品名称', prop: 'productName' },
        { label: '受理日期', prop: 'createDate' },
        {
          label: '审核状态',
          prop: 'status',
          render: (value, row) => {
            return <span>{CREDITEXTENSION_OFFLINE_STATUS[row.status]}</span>
          },
        },
        { label: '来源', prop: 'appName' },
        {
          label: '操作',
          render: (value, row) => {
            return (
              <div>
                {isCanAudit && row.status === '0' && row.appCode !== 'WMR' ? (
                  <el-button
                    type="text"
                    onClick={() => {
                      this.audit(row)
                    }}>
                    审核
                  </el-button>
                ) : (
                  ''
                )}
                {isCanDetail && (
                  <el-button
                    type="text"
                    onClick={() => {
                      this.jumpToDetail(row)
                    }}>
                    详情
                  </el-button>
                )}
              </div>
            )
          },
        },
      ],
    }
  },
  methods: {
    audit(item) {
      //状态需区别：弹窗展示不同
      this.$refs.CreditExtensionPop.show({ ...item, title: '审批', auditType: 1, maxMoney: item.fundQuota })
    },
    jumpToDetail(item) {
      this.$router.push({
        name: 'creditExtensionDetail',
        query: {
          applyId: item.id,
          appCode: item.appCode,
          uscCode: item.uscCode || '',
        },
      })
    },
    addEditSuccess(isClose) {
      if (isClose) {
        this.$refs.search.$refs.searchForm.resetFields()
        this.form.onSubmit()
      }
    },
    //获取来源
    async getSourceListRequest() {
      const sourceList = await this.$http.get(`/app/list`)
      const list = sourceList.map(item => {
        return {
          label: item.name,
          value: item.code,
        }
      })
      this.searchConfig[1].options = [...list]
    },
  },
  mounted() {
    this.getSourceListRequest()
  },
}
</script>

<style lang="scss" scoped></style>
